package blog

import (
	. "enca/module/db"
)

//博客列表
type BlogList struct {
	Id          int       `json:"id,omitempty" form:"id" db:"id"`
	Title       string    `json:"title,omitempty" form:"title" db:"title"`
	Content     string    `json:"content,omitempty" form:"content" db:"content"`
	ReleaseTime string    `json:"releaseTime,omitempty" form:"release_time" db:"release_time"`
	UpdateTime  string    `json:"updateTime,omitempty" form:"update_time" db:"update_time"`
	State       string    `json:"state,omitempty" form:"state" db:"state"`
	SeeNum      string    `json:"seeNum,omitempty" form:"see_num" db:"see_num"`
	Comment     []Comment `json:"comment,omitempty" form:"comment" db:"comment"`
}

//博客评论
type Comment struct {
	CommentId      int    `json:"commentId,omitempty" form:"comment_id" db:"comment_id"`
	BlogId         int    `json:"blogId,omitempty" form:"blog_id" db:"blog_id"`
	CommentName    string `json:"commentName,omitempty" form:"comment_name" db:"comment_name"`
	CommentContent string `json:"commentContent,omitempty" form:"comment_content" db:"comment_content"`
	CommentTime    string `json:"commentTime,omitempty" form:"comment_time" db:"comment_time"`
}

//画板列表
type DrawingList struct {
	Id   int    `json:"id,omitempty" form:"id" db:"id"`
	Src  string `json:"src,omitempty" form:"src" db:"src"`
	Name string `json:"name,omitempty" form:"name" db:"name"`
	Time string `json:"time,omitempty" form:"time" db:"time"`
}

//博客首页配置
type HomeConfig struct {
	Id    int    `json:"id,omitempty" form:"id" db:"id"`
	Bg    string `json:"bg,omitempty" form:"bg" db:"bg"`
	Title string `json:"title,omitempty" form:"title" db:"title"`
	Descs string `json:"descs,omitempty" form:"descs" db:"descs"`
	Other string `json:"other,omitempty" form:"other" db:"other"`
}

//错误
func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}

func Blogs() (code int, data interface{}) {
	blogList := make([]BlogList, 0)

	berr := Db.Select(&blogList, "SELECT * FROM blog_list  WHERE state = 1 order by release_time desc")
	checkErr(berr)

	comment := make([]Comment, 0)

	cerr := Db.Select(&comment, "SELECT * FROM blog_comment_list order by comment_time desc")
	checkErr(cerr)

	resBlogList := make([]BlogList, 0)

	for blogIndex, blogItem := range blogList {

		resCommentList := make([]Comment, 0)

		for _, commentItem := range comment {

			if commentItem.BlogId == blogItem.Id {

				resCommentList = append(resCommentList, commentItem)

			}

		}

		resBlogList = append(resBlogList, blogItem)

		resBlogList[blogIndex].Comment = resCommentList
	}

	if len(resBlogList) > 0 {
		code = 200

		data = resBlogList
	} else {
		code = 500

		data = "请求失败"
	}

	return
}

func CommentAdd(id string, name string, content string, time string) (code int, data interface{}) {
	info, _ := Db.Prepare(`INSERT blog_comment_list (blog_id, comment_name, comment_content, comment_time) values (?, ?, ?, ?)`)

	_, err := info.Exec(id, name, content, time)
	checkErr(err)

	code = 200

	data = "评论成功"

	return
}

func BlogDetail(id string) (code int, data interface{}) {
	//在此做查看次数的增加
	upinfo, _ := Db.Prepare("UPDATE blog_list set see_num=see_num+1 where id=?")

	_, uperr := upinfo.Exec(id)
	checkErr(uperr)

	blogList := make([]BlogList, 0)

	err := Db.Select(&blogList, "SELECT * FROM blog_list where id="+id+" order by release_time desc")
	checkErr(err)

	comment := make([]Comment, 0)

	cerr := Db.Select(&comment, "SELECT * FROM blog_comment_list  where blog_id="+id+" order by comment_time desc")
	checkErr(cerr)

	blogList[0].Comment = comment

	if len(blogList) > 0 {
		code = 200

		data = blogList[0]
	} else {
		code = 500

		data = "请求错误"
	}

	return
}

func UploadImageDrawing(src string, name string, time string) (code int, data string) {
	info, _ := Db.Prepare(`INSERT drawing_list (src, name, time) values (?, ?, ?)`)

	_, ierr := info.Exec(src, name, time)
	checkErr(ierr)

	code = 200

	data = src

	return
}

func Drawings() (code int, data interface{}) {
	var drawingList []DrawingList

	err := Db.Select(&drawingList, "SELECT * FROM drawing_list order by time desc")
	checkErr(err)

	code = 200

	data = drawingList

	return
}

func IndexConfig() (code int, data interface{}) {
	var homeConfig []HomeConfig

	err := Db.Select(&homeConfig, "SELECT * FROM blog_config")
	checkErr(err)

	if len(homeConfig) > 0 {
		code = 200
		data = homeConfig[0]
	} else {
		code = 500
		data = "获取配置信息失败"
	}

	return
}
